#!/bin/sh

# Test will start rtpproxy, initiate a stream playing out,
# capture and decode packers them with extractaudio and verifies the
# checksums for all supported codecs.

BASEDIR="`dirname "${0}"`/.."
SUDO_REQUIRED=1
. "${BASEDIR}/functions"

TARGET_PORT=12344
RTPP_TIMEOUT=8
PLAYBACK_RFILES="playback1.0 playback1.3 playback1.8 playback1.18 playback1.9"
rm -f ${PLAYBACK_RFILES}
${MAKEANN} ${BASEDIR}/ringback.sln ${BASEDIR}/playback1
report "makeann playback1"

setup_playback() {
  delay="${1}"
  codec="${2}"
  target_port="${3}"
  pifile="${4}"
  p2ifile="${5}"
  sleep ${delay}
  ${SED} "s|%%CODEC%%|${codec}| ; s|%%PORT1%%|${target_port}| ; s|%%PORT2%%|$((${target_port} + 2))|" "${pifile}"
  if [ "${p2ifile}" != "" ]
  then
    sleep 1
    ${SED} "s|%%CODEC%%|${codec}| ; s|%%PORT1%%|${target_port}| ; s|%%PORT2%%|$((${target_port} + 2))|" "${p2ifile}"
  fi
  sleep 1
  ${SED} "s|%%CODEC%%|${codec}| ; s|%%PORT1%%|${target_port}| ; s|%%PORT2%%|$((${target_port} + 2))|" playback/playback1.nat.input
}

for ttype in 1 2 3 4 5 6 7
do
  if [ ${ttype} -eq 1 ]
  then
    delay=0
  else
    delay=1
  fi
  PIFILE="playback/playback1.type${ttype}.input"
  if [ -e "playback/playback1.type${ttype}.st2.input" ]
  then
    P2IFILE="playback/playback1.type${ttype}.st2.input"
  else
    P2IFILE=""
  fi
  for codec in 0 3 8 18 9
  do
    if [ ${codec} -ne 3 ]
    then
      NPKTS=600
    else
      NPKTS=300
    fi
    if [ ${ttype} -eq 3 ]
    then
      NPKTS=100
    fi
    NPKTS=$((${NPKTS} * 2)) # IPv6
    for retry in 1 2 3 4 5 6
    do
      for fname in playback1.tout playback1.${codec}.a.rtp
      do
        if [ -e "${fname}" ]
        then
          rm -f "${fname}"
        fi
      done
      ${SUDO} true || return 1
      ${SUDO} ${TCPDUMP} -B 4096 -w playback1.${codec}.a.rtp -s0 -ni ${LOOPBACK_INTERFACE} -c ${NPKTS} udp port ${TARGET_PORT} 2>playback1.tout &
      TCPD_PID=${!}
      while [ ! -e playback1.${codec}.a.rtp ]
      do
        sleep 1
      done
      if [ ${delay} -eq 0 ]
      then
        sleep 1
      fi
      setup_playback ${delay} ${codec} ${TARGET_PORT} "${PIFILE}" "${P2IFILE}" | \
       ${RTPPROXY} -l 0.0.0.0 -6 /:: -T$((${RTPP_TIMEOUT} + ${delay})) -b -m 12000 -s stdio: -f -d info > playback1.rout 2>playback1.rlog&
      PLB_PID=${!}
      ##ktrace -p ${PLB_PID} -f /var/tmp/rtpproxy.${PLB_PID}.ktrace
      ##echo ${TCPD_PID}
      wait ${PLB_PID}
      report "wait for rtpproxy shutdown, PID: ${PLB_PID}"
      ${SUDO} kill -TERM ${TCPD_PID} 2>/dev/null
      KILL_RC=${?}
      wait ${TCPD_PID}
      TCPD_RC=${?}
      if [ ${KILL_RC} -ne 0 -a ${TCPD_RC} -eq 0 ]
      then
        ok "wait for tcpdump shutdown, PID: ${TCPD_PID}"
        break
      fi
      ncaptured=`grep 'packets captured' playback1.tout | awk '{print $1}'`
      if [ ${ncaptured} -eq ${NPKTS} ]
      then
        ok "wait for tcpdump shutdown, PID: ${TCPD_PID}, kill rc = ${KILL_RC}, tcpdump rc = ${TCPD_RC}"
        break
      fi
      cat playback1.tout
      ndropped=`grep 'dropped by kernel' playback1.tout | awk '{print $1}'`
      if [ ${ndropped} -ne 0 -a ${retry} -lt 6 ]
      then
        report "..kernel dropped some packets, re-run"
        continue
      fi
      ${EXTRACTAUDIO} -n playback1.${codec} /dev/null
      forcefail 1 "wait for tcpdump shutdown, PID: ${TCPD_PID}, kill rc: ${KILL_RC}, tcpd rc: ${TCPD_RC}"
    done
    cat playback1.tout
    ofile="playback1.${codec}.wav"
    ${EXTRACTAUDIO} -n playback1.${codec} ${ofile} | grep -v 'delta '
    report "extractaudio playback1.${codec} -> ${ofile}"
    #sha256 ${ofile}
    #report "sha256 ${ofile}"
    #wait
    if [ -e playback/playback1.type${ttype}.output ]
    then
      MODEL_ROUT=playback/playback1.type${ttype}.output
    else
      MODEL_ROUT=playback/playback1.output
    fi
    ${DIFF} playback1.rout ${MODEL_ROUT}
    report "checking rtpproxy stdout"
    sha256_verify ${ofile} playback/playback1.checksums
  done
done
